<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="username" label="用户名">
				<uni-easyinput placeholder="用户名，不允许重复" disabled v-model="formData.username" trim="both" />
			</uni-forms-item>
			 <uni-forms-item label-width="200" name="type" label="用户类型">
				<el-radio-group v-model="realname_auth.type">
				    <el-radio :label="0">个人用户</el-radio>
				    <el-radio :label="1">企业用户</el-radio>
				  </el-radio-group>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="auth_status" label="认证状态">
				<el-radio-group v-model="realname_auth.auth_status">
				    <el-radio :label="0"> 未认证</el-radio>
				    <el-radio :label="1">等待认证</el-radio>
				    <el-radio :label="2">认证通过</el-radio>
				    <el-radio :label="3">认证失败</el-radio>
				  </el-radio-group>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="auth_date" label="认证通过时间">
				<uni-easyinput placeholder="认证通过时间" v-model="realname_auth.auth_date" />
			</uni-forms-item>
			<uni-forms-item label-width="200" name="real_name" label="真实姓名/企业名称">
				<uni-easyinput placeholder="真实姓名/企业名称" v-model="realname_auth.real_name" />
			</uni-forms-item>
			<uni-forms-item label-width="200" name="identity" label="身份证号码/营业执照号码">
				<uni-easyinput placeholder="身份证号码/营业执照号码" v-model="realname_auth.identity" />
			</uni-forms-item>
			<uni-forms-item label-width="200" name="id_card_front" label="身份证正面照">
				<cloud-image placeholder="身份证正面照" v-model="realname_auth.id_card_front"></cloud-image>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="id_card_back" label="身份证反面照">
				<cloud-image placeholder="身份证反面照" v-model="realname_auth.id_card_back"></cloud-image>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="in_hand" label="手持身份证照片">
				<cloud-image placeholder="手持身份证照片" v-model="realname_auth.in_hand"></cloud-image>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="license" label="营业执照">
				<cloud-image placeholder="营业执照" v-model="realname_auth.license"></cloud-image>
			</uni-forms-item>
			<uni-forms-item label-width="200" name="contact_person" label="联系人姓名">
				<uni-easyinput placeholder="联系人姓名" v-model="realname_auth.contact_person" />
			</uni-forms-item>
			<uni-forms-item label-width="200" name="contact_mobile" label="联系人手机号码">
				<uni-easyinput placeholder="联系人手机号码"  v-model="realname_auth.contact_mobile" />
			</uni-forms-item>
			<uni-forms-item label-width="200" name="contact_email" label="联系人邮箱">
				<uni-easyinput placeholder="联系人邮箱" v-model="realname_auth.contact_email" />
			</uni-forms-item>
			<view class="uni-button-group"><button type="primary" class="uni-button" @click="submit">提交</button></view>
		</uni-forms>
	</view>
</template>

<script>
import { validator } from '../../js_sdk/validator/uni-id-users.js';

const db = uniCloud.database();
const dbCollectionName = 'uni-id-users';

function getValidator(fields) {
	let reuslt = {};
	for (let key in validator) {
		if (fields.indexOf(key) > -1) {
			reuslt[key] = validator[key];
		}
	}
	return reuslt;
}

export default {
	data() {
		return {
			realname_auth:{
				type: 0,
				auth_status: '',
				auth_date: '',
				real_name: '',
				identity: '',
				id_card_front: '',
				id_card_back: '',
				in_hand: '',
				license: '',
				contact_person: '',
				contact_mobile: '',
				contact_email: ''
			},
			formData: {
				username: '',
				password_secret_version: null,
				nickname: '',
				gender: 0,
				status: 0,
				mobile: '',
				mobile_confirmed: 0,
				email: '',
				email_confirmed: 0,
				avatar: '',
				department_id: [],
				wx_unionid: '',
				wx_openid: null,
				ali_openid: '',
				apple_openid: '',
				comment: '',
				last_login_date: null,
				last_login_ip: '',
				token: [],
				inviter_uid: [],
				my_invite_code: '',
				paramedic_profile: null,
				realname_auth: {
					type: '',
					auth_status: '',
					auth_date: '',
					real_name: '',
					identity: '',
					id_card_front: '',
					id_card_back: '',
					in_hand: '',
					license: '',
					contact_person: '',
					contact_mobile: '',
					contact_email: ''
				},
			},
			formOptions: {
				gender_localdata: [
					{
						text: '未知',
						value: 0
					},
					{
						text: '男',
						value: 1
					},
					{
						text: '女',
						value: 2
					}
				],
				status_localdata: [
					{
						text: '正常',
						value: 0
					},
					{
						text: '禁用',
						value: 1
					},
					{
						text: '审核中',
						value: 2
					},
					{
						text: '审核拒绝',
						value: 3
					}
				],
				mobile_confirmed_localdata: [
					{
						text: '未验证',
						value: 0
					},
					{
						text: '已验证',
						value: 1
					}
				],
				email_confirmed_localdata: [
					{
						text: '未验证',
						value: 0
					},
					{
						text: '已验证',
						value: 1
					}
				]
			},
			rules: {
				...getValidator([
					'username',
					'password_secret_version',
					'nickname',
					'gender',
					'status',
					'mobile',
					'mobile_confirmed',
					'email',
					'email_confirmed',
					'avatar',
					'department_id',
					'wx_unionid',
					'wx_openid',
					'ali_openid',
					'apple_openid',
					'comment',
					'realname_auth',
					'last_login_date',
					'last_login_ip',
					'token',
					'inviter_uid',
					'my_invite_code',
					'paramedic_profile'
				])
			}
		};
	},
	onLoad(e) {
		const id = e.id;
		this.formDataId = id;
		this.getDetail(id);
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
	},
	methods: {
		/**
		 * 触发表单提交
		 */
		submit() {
			uni.showLoading({
				mask: true
			});
			this.$refs.form
				.submit()
				.then(res => {
					this.submitForm({'realname_auth':this.realname_auth});
				})
				.catch(errors => {
					uni.hideLoading();
				});
		},

		submitForm(value) {
			// 使用 clientDB 提交数据
			db.collection(dbCollectionName)
				.doc(this.formDataId)
				.update(value)
				.then(res => {
					uni.showToast({
						icon: 'none',
						title: '修改成功'
					});
					this.getOpenerEventChannel().emit('refreshData');
					setTimeout(() => uni.navigateBack(), 500);
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		},

		/**
		 * 获取表单数据
		 * @param {Object} id
		 */
		getDetail(id) {
			uni.showLoading({
				mask: true
			});
			db.collection(dbCollectionName)
				.doc(id)
				.field(
					'username,realname_auth,nickname,gender,status,mobile,mobile_confirmed,email,email_confirmed,avatar,department_id,wx_unionid,wx_openid,ali_openid,apple_openid,comment,realname_auth,last_login_date,last_login_ip,token,inviter_uid,my_invite_code'
				)
				.get()
				.then(res => {
					const data = res.result.data[0];
					if (data) {
						this.formData = data;
						if(data.realname_auth){
							this.realname_auth=data.realname_auth
						}
					}
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		}
	}
};
</script>

<style>
.uni-container {
	padding: 15px;
}
.uni-app--showleftwindow .uni-container .uni-forms {
	max-width: 700px;
}
.uni-input-border,
.uni-textarea-border {
	width: 100%;
	font-size: 14px;
	color: #666;
	border: 1px #e5e5e5 solid;
	border-radius: 5px;
	box-sizing: border-box;
}

.uni-input-border {
	padding: 0 10px;
	height: 35px;
}

.uni-textarea-border {
	padding: 10px;
	height: 80px;
}

.uni-button-group {
	margin-top: 50px;
	display: flex;
	justify-content: center;
}

.uni-button {
	width: 184px;
	padding: 12px 20px;
	font-size: 14px;
	border-radius: 4px;
	line-height: 1;
	margin: 0;
}
</style>
